<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$data 		= '';
$error		= '';
$start		= $_REQUEST['start'];
$end		= $_REQUEST['end']
;$client_id	= $_REQUEST['id'];
		
	switch ($action) {	
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    $rResult=mysql_query("    	        
    SELECT  
			e_letter_details.warehouse_detail_id  AS wd_id,
			IF(SUBSTR(warehouse_detail.barcode, 1, 1)=1, 'რეზერვი', 'კლიენტი') AS `type`,
			e_letter_details.barcode,
			barcodes.serial_number,
			production.`name`,
			CONCAT('ე - ' , d_letter_id),   
			d_letter.fact_end_date,
			client.`name`,
			printer_parts_type.`name` AS `service`,
    	    '&#32;',    
			FORMAT(price,2),
			FORMAT(self_cost,2)		  
FROM  warehouse_detail
JOIN  e_letter_details 		ON warehouse_detail.barcode 	=	e_letter_details.barcode AND warehouse_detail.id=e_letter_details.warehouse_detail_id
JOIN  d_letter 				 		ON d_letter.id 	= e_letter_details.d_letter_id	
JOIN  barcodes 				 		ON e_letter_details.barcode=barcodes.`code`
JOIN  production 			 		ON warehouse_detail.prod_id=production.id
JOIN  client_objects 	 		ON client_objects.id = d_letter.client_object_id
JOIN  client 					  	ON client.id  = client_objects.client_id
JOIN  printer_parts_type 	ON printer_parts_type.id = part_type_id
 WHERE DATE(d_letter.fact_end_date) BETWEEN '$start' AND '$end'
UNION 
SELECT       warehouse_detail.id AS wd_id,
			 CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",IF(SUBSTR(warehouse_detail.barcode, 1, 1)=1, 'რეზერვი', 'კლიენტი'),'</div>')  AS `type`,
			 CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",warehouse_detail.barcode,'</div>') ,
			 CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",barcodes.serial_number,'</div>') ,
             CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",production.`name`,'</div>') ,
             CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",CONCAT('ე - ' , d_letter.id),'</div>') ,   
			 CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",d_letter.fact_end_date,'</div>') ,
			 CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",client.`name`,'</div>') ,
    	     CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",'&#32;','</div>')  AS `service1`,   
			 CONCAT(\"<div style='width: 150%; text-align: left; background: rgba(0, 255, 3, 0.2)'>\",printer_parts_type.`name`,'</div>')  AS `service`,
			('0.00'),
			 FORMAT(IFNULL((SELECT price FROM reserve WHERE reserve.production_id=real_history.part_prod_id LIMIT 1),0),2)
FROM real_history 
JOIN warehouse_detail on warehouse_detail.id=real_history.warehouse_id AND warehouse_detail.actived=1 
JOIN  production 			ON warehouse_detail.prod_id=production.id
JOIN  warehouse 			ON warehouse.id 	= warehouse_detail.warehouse_id 
JOIN  barcodes 				ON warehouse_detail.barcode=barcodes.`code`
JOIN  d_letter				ON warehouse.d_letter_id=d_letter.id	AND warehouse.actived=1
JOIN  client_objects 	 		ON client_objects.id = d_letter.client_object_id AND client_objects.actived=1
JOIN  client 					  	ON client.id  = client_objects.client_id AND client.actived=1
JOIN  printer_parts_type 	ON printer_parts_type.id = real_history.printer_part_id
 WHERE DATE(d_letter.fact_end_date) BETWEEN '$start' AND '$end'
    	        
 ORDER BY wd_id   	        
    	        
    	        
    	       
    	                            
                                    	        ");
			
			$data = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}

	        break;
	    case 'get_edit_page':
	        $table="<table class='table_style'>
                        <tr >
                            <td>სერვისი </td>
                            <td>ღირებულება</td>
                            <td>თვითღირებულება</td>                             
                            <td>რეალური თვითღირებულება</td>
                        </tr>";
	        $query=mysql_query("SELECT printer_parts_type.`name` AS `service`, 
		 	                    price,
	                            self_cost,
		 	 		 	       (SELECT price FROM reserve WHERE reserve.production_id=real_history.part_prod_id LIMIT 1)
                                FROM  printer_parts_type
                                LEFT JOIN   real_history	ON printer_parts_type.id = real_history.printer_part_id AND real_history.warehouse_id=$_REQUEST[id]
                                LEFT JOIN   e_letter_details	ON printer_parts_type.id = e_letter_details.part_type_id AND warehouse_detail_id=$_REQUEST[id] 
                                WHERE IFNULL(real_history.id,e_letter_details.id) IS NOT NULL");
       while ($row = mysql_fetch_array($query)) {
           $table.="<tr>
                        <td>$row[0]</td>
                        <td>$row[1]</td>
                        <td>$row[2]</td>
                        <td>$row[3]</td>                      
                    </tr>";
       }             	        
$table.="
</table>";
	        
	        $data[page]=$table;
	        break;   	    
	    default:
	       $error = "action null";
	}

$data['error'] = $error;

echo json_encode($data);

?>